package com.ygqh.baby.service.impl;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.*;
import com.ygqh.baby.mapper.YgActionAddressMapper;
import com.ygqh.baby.model.YgSkuInfo;
import com.ygqh.baby.po.YgActionAddress;
import com.ygqh.baby.po.YgActionAddressExample;
import com.ygqh.baby.po.YgUserAddress;
import com.ygqh.baby.service.*;
import com.ygqh.baby.service.wdtqyb.api.WdtQybApi;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class YgActionAddressServiceImpl implements YgActionAddressService {
	protected final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private YgActionAddressMapper ygActionAddressMapper;
	@Autowired
	private YgFrameCodeService ygFrameCodeService;
	@Autowired
	private YgProductService ygProductService;
	@Autowired
	private YgSkuService ygSkuService;
	@Autowired
	private YgProductImagesService ygProductImagesService;
	@Autowired
	private YgOrderService ygOrderService;
	@Autowired
	private YgOrderLogService ygOrderLogService;
	@Autowired
	private YgOrderDetailService ygOrderDetailService;
	@Autowired
	private YgStockService ygStockService;
	@Autowired
	private WdtQybApi wdtQybApi;

	@Override
	public List<YgActionAddress> find() {
		YgActionAddressExample example = new YgActionAddressExample();
		return ygActionAddressMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgActionAddressExample example = new YgActionAddressExample();
		return ygActionAddressMapper.countByExample(example);
	}

	@Override
	public YgActionAddress findById(Long id) {
		return ygActionAddressMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgActionAddress ygActionAddress) {
		return ygActionAddressMapper.insertSelective(ygActionAddress);
	}

	@Override
	public int update(YgActionAddress ygActionAddress) {
		return ygActionAddressMapper.updateByPrimaryKeySelective(ygActionAddress);
	}

	@Override
	public ResultSet<YgActionAddress> search(QueryInfo queryInfo, String userName, Date startDate, Date endDate, Boolean isSync, String q) {
		List<YgActionAddress> list = ygActionAddressMapper.selectSuper(queryInfo, userName, startDate, endDate, isSync, q);
		int count = ygActionAddressMapper.countSuper(userName, startDate, endDate, isSync, q);
		ResultSet<YgActionAddress> resultSet = new ResultSet<YgActionAddress>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgActionAddressExample example = new YgActionAddressExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgActionAddress record = new YgActionAddress();

		return ygActionAddressMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgActionAddress ygActionAddress) {
		int r = 0;
		if (ygActionAddress.getId() != null) {
			r = this.update(ygActionAddress);
		} else {
			r = this.save(ygActionAddress);
		}
		return r;
	}

	@Override
	public Message saveActionOrder(Long actionId) {
		YgActionAddress action = this.findById(actionId);
		if (StringUtil.isBlank(action.getAddress())) {
			return Message.error("");
		}
		String address = action.getAddress().replace(",", ",").replace("，", ",");
		String[] split = address.split(",");
		if (split.length != 4) {
			return Message.error("请确认地址是否有误");
		}
		YgSkuInfo skuInfo = ygSkuService.findSkuInfoBySkuCode(action.getSkuCode(), PromotionType.Ordinary);
		skuInfo.setQuantity(1L);
		YgUserAddress ygAddress = new YgUserAddress(action.getUserId(), action.getReceiver(),
				action.getTelphone(), split[0], split[1], split[2], null,
				address.replace(",", " "));
		return ygOrderService.saveZeroOrder(skuInfo, ygAddress, null, AppType.WeChat);
	}

	@Override
	public Message syncAction(Long actionAddrId) {
		YgActionAddress ygActionAddress = this.findById(actionAddrId);
		Long orderId = ygActionAddress.getOrderId();
		Boolean isSync = ygActionAddress.getIsSync();
		if (orderId == null && !isSync) {
			Message message = this.saveActionOrder(actionAddrId);
			orderId = (Long) message.getExtra();
			ygActionAddress.setOrderId(orderId);
			this.update(ygActionAddress);
		} else {
			Message.error("请不要重复同步", "");
		}

		return this.doSyncAction(ygActionAddress.getId(), orderId);
	}

	private Message doSyncAction(Long actionAddrId, Long orderId) {

		Long[] orderIds = new Long[] { orderId };
		Message msg = wdtQybApi.tradePushByOrderIds(Arrays.asList(orderIds));
		if (msg.getType().equals(Message.Type.error)) {
			return msg;
		}
		YgActionAddress ygActionAddress = new YgActionAddress();
		ygActionAddress.setId(actionAddrId);
		ygActionAddress.setIsSync(true);
		this.update(ygActionAddress);
		return Message.success("");

	}
}
